<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>攻略文章</title>
	<#include "../common/header.ftl" >

    <script src="/static/jquery/jquery-form/jquery.form.js"></script>
    <script src="/static/jquery/jquery-bootstrap/jquery.bootstrap.min.js"></script>
    <script src="/static/My97DatePicker/WdatePicker.js"></script>
    <script src="/static/jquery/ckeditor/ckeditor.js"></script>

    <style>
        .modal-body img {
            width: 100%;
        }
    </style>


    <script>
        $(function () {
            //添加触发模态框
            $('#addStrategyBtn').click(function () {
                //清空回显
                $('#strategyDetailModal input').val('');
                $('#strategyDetailModal .btn-changePic').val('更换图片');
               /* $('#strategyDetailModal #modal-coverUrl').attr('src'," ");*/
                $('#strategyDetailModal #modal-coverUrl').remove();
                $('#strategyDetailModal [name="coverUrl"]').before('<img width="200px" id="modal-coverUrl"/>');
                $('#strategyDetailModal').modal('show');
            });

            //修改功能
            $('.btn-recomment').click(function () {
                var json = $(this).data('json');
                $('#strategyDetailModal #id').val(json.id);
                $('#strategyDetailModal [name="title"]').val(json.title);
                $('#strategyDetailModal #strategySelect').val(json.strategyId);
                $('#strategyDetailModal [name="sequence"]').val(json.sequence);
                $('#strategyDetailModal [name="state"]').val(json.state);
                $('#strategyDetailModal [name="coverUrl"]').val(json.coverUrl);
                $('#strategyDetailModal #modal-coverUrl').attr('src',json.coverUrl);

                //发请求查询其攻略分类
                $.get('/strategyCatalog/getByStrategyId.do',{strategyId:json.strategyId},function (data) {
                    var temp="";
                    $.each($(data),function (index,value) {
                        //拼接
                        temp +='<option value="'+ value.id+'">'+value.name+'</option>';
                    });
                    $('#strategyCatalogSelect').html(temp);

                    //回显攻略分类  其要在异步请求中
                    $('#strategyDetailModal #strategyCatalogSelect').val(json.strategyCatalogId);
                });

                //发请求查询相应的文章内容
                $.get('/strategyDetail/getContentById.do',{id:json.id},function (data) {
                    //回显文本内容
                    ck.setData(data.content);
                });

                $('#strategyDetailModal').modal('show');
            });

            //提交添加/修改表单
            $('#saveCommendBtn').click(function () {
                //获取富文本内容并设置到表单的文本域
                var data = ck.getData();
                $('#editor').html(data);
                //提交表单
                $('#commendForm').ajaxSubmit(function (data) {
                    if (data.success) {
                        $.messager.alert('温馨提示', '操作成功!  2s后刷新页面');
                        setTimeout(function () {
                            window.location.reload();
                        }, 2000);
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                });
            });

            $('#strategySelect').change(function () {
                //获取攻略id
                var id = $(this).val();
                if(id!=-1){
                    //发请求查询其攻略分类
                    $.get('/strategyCatalog/getByStrategyId.do',{strategyId:id},function (data) {
                        var temp="";
                        $.each($(data),function (index,value) {
                            //拼接
                            temp +='<option value="'+ value.id+'">'+value.name+'</option>';
                        });
                        $('#strategyCatalogSelect').html(temp)
                    })
                }
            });

            //更换封面按钮触发文件上传框事件
            $('.btn-changePic').click(function () {
                //触发文件上传框
                $('#fileForm [name=file]').click();
            });

            //当文件上传框的值改变就发请求更新
            $('#fileForm').change(function () {
                //判断是否有上传文件
                if($('#fileForm [name=file]').val()){
                    //提交表单
                    $('#fileForm').ajaxSubmit(function (data) {
                        //根据返回结果判断上传结果
                        if(data.status==1){
                            $('#strategyDetailModal [name="coverUrl"]').val(data.url);
                            $('#strategyDetailModal #modal-coverUrl').attr('src',data.url);
                        }else{
                            $(document).dialog({
                                content:data.msg
                            })
                        }
                    })
                }
            })

        })
    </script>
</head>
<body>

<div class="container " style="margin-top: 20px">
	<#include "../common/top.ftl" >
    <div class="row">
        <div class="col-sm-3">
            <!--设置用于菜单回显-->
                <#assign currentMenu="strategyDetail">
			<#include "../common/menu.ftl" >
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">大攻略管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/strategyDetail/list.do" method="post">
                <div class="form-group">
                    <input id="currentPage" type="hidden" name="currentPage" value="${(qo.currentPage)!}">
                    <label for="keyword">关键字:</label>
                    <input type="text" class="form-control" id="keyword" name="keyword" value="${(qo.keyword)!}"
                           placeholder="请输入标题">
                </div>

                <input type="submit" id="btn_query" class="btn btn-success" value="查询">

            </form>

            <button class="btn btn-success" id="addStrategyBtn">添加攻略文章</button>

            <table class="table table-striped table-hover">
                <thead>
                <tr>
                    <th>标题</th>
                    <th>封面</th>
                    <th>发布时间</th>
                    <th>排序</th>
                    <th>攻略类别</th>
                    <th>状态</th>

                </tr>
                </thead>
				<#list  pageInfo.list as strategyDetail>
					<tr>
                        <td>${(strategyDetail.title)!}</td>
                        <td>
                            <img src='${(strategyDetail.coverUrl)!}' width="50px">
                        </td>
                        <td>${(strategyDetail.releaseTime?string('yyyy-MM-dd HH:mm:ss'))!}</td>
                        <td>${(strategyDetail.sequence)!}</td>
                        <td>${(strategyDetail.strategyCatalog.name)!}</td>
                        <td>${(strategyDetail.stateName)!}</td>
                        <td>
                            <button class="btn btn-info btn-xs btn-recomment"
                                    data-json='${(strategyDetail.jsonString)!}'>
                                修改
                            </button>

                        </td>
                    </tr>
                </#list>
            </table>

			<#include "../common/page.ftl" >
        </div>
    </div>
</div>


<#--推荐模态框-->
<div id="strategyDetailModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">编辑/增加</h4>
            </div>
            <div class="modal-body">
                <form id="commendForm" class="form-horizontal" method="post" action="/strategyDetail/saveOrUpdate.do"
                      style="margin: -3px 118px">
                    <input id="id" type="hidden" name="id" value=""/>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">标题</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" name="title" placeholder="请输入标题">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">封面</label>
                        <div class="col-sm-9">
                            <img width="200px" id="modal-coverUrl"/>
                            <input type="hidden" name="coverUrl"  >
                            <input TYPE="button" class="btn btn-info btn-xs btn-changePic" VALUE="更换图片" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">所属攻略</label>
                        <div class="col-sm-9">
                            <select id="strategySelect" name="strategy.id" class="form-control form-control-chosen">
                                <option value="-1">请选择所属攻略</option>
                                <#list strategies as strategy>
                                    <option value="${(strategy.id)!}">${(strategy.title)!}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">所属类别</label>
                        <div class="col-sm-9">
                            <select id="strategyCatalogSelect" name="strategyCatalog.id" class="form-control form-control-chosen">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">状态</label>
                        <div class="col-sm-9">
                            <select class="form-control" autocomplete="off" name="state">
                                <option value="0">草稿</option>
                                <option value="1">发布</option>
                                <option value="-1">禁用</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">排序</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" name="sequence" placeholder="排序">
                        </div>
                    </div>
                    <div class="form-group">
                        <textarea id="editor" name="strategyContent.content" class="ckeditor" cols="10" rows="10">
                        </textarea>
                        <script>
                            var ck = CKEDITOR.replace('editor');
                        </script>
                    </div>
                </form>
                <#--隐藏的图片表单-->
                <form id="fileForm" action="/images.do" method="post" enctype="multipart/form-data">
                    <input type="file" class="form-control"  name="file" style="display: none">
                </form>
            </div>
            <div class="modal-footer">
                <a href="javascript:void(0);" class="btn btn-success" id="saveCommendBtn" aria-hidden="true">保存</a>
                <a href="javascript:void(0);" class="btn" data-dismiss="modal" aria-hidden="true">关闭</a>
            </div>
        </div>
    </div>
</div>


</body>
</html>